#include "PawnInfo.h"
#include "ConfigManager.h"
#include "CCSpriteFrameCache.h"
#include "AnimatePacker.h"
#include "StringConvert.h"

PawnInfo::PawnInfo()
{

}

PawnInfoSet::PawnInfoSet()
{
	
}

void PawnInfoSet::loadRaceResource(RaceType raceType1, RaceType raceType2)
{
	Config* config = ConfigManager::instance()->getConfig("pawn");

	Config::DataTable& dataTable = config->getItems();
	Config::DataTable::iterator it = dataTable.begin();
	Config::DataTable::iterator itend = dataTable.end();

	for(; it != itend; it++)
	{
		const std::string& pawnIDStr = it->first;
		Config::DataItem& dataItem = it->second;
		const std::string& race = dataItem["race"];
		const std::string& type = dataItem["type"];

		PawnInfo info;

		info.mRaceType = getRaceTypeFromStr(race);
		assert(info.mRaceType != RaceType_Invalid);

		if(info.mRaceType == raceType1 || info.mRaceType == raceType2)
		{
			info.m_type = getPawnTypeFromStr(type);
			assert(info.m_type != PawnType_Invalid);

			info.m_textureName = dataItem["path"];
			info.mDefaultAnim = dataItem["defaultAnim"];
			info.mPawnIDStr = dataItem["pawnID"];
			info.m_hp = StringConvert::stringToInt(dataItem["hp"]);
			info.m_attack = StringConvert::stringToInt(dataItem["ap"]);
			info.m_holdTimes = StringConvert::stringToInt(dataItem["sp"]);

			if(info.m_type == Pawn_Wall)
			{
				info.mWallInfo.mAutoRegenerate = StringConvert::stringToBool(dataItem["ar"]);
				info.mWallInfo.mHPIncrease = StringConvert::stringToInt(dataItem["hpIncr"]);
				info.mWallInfo.mHPMax = StringConvert::stringToInt(dataItem["maxhp"]);
			}

			PawnInfoList& infoList = m_racePawnInfos[info.mRaceType];
			std::vector<PawnInfo>& pawnInfoList = infoList[info.m_type];

			pawnInfoList.push_back(info);

			char name[100] = {0};

			CCSpriteFrameCache* spriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();

			if(info.m_type == Pawn_Wall)
			{
				spriteFrameCache->addSpriteFramesWithFile((info.m_textureName + ".plist").c_str(), (info.m_textureName + ".png").c_str());
				AnimatePacker::getInstance()->loadAnimations((info.m_textureName + ".xml").c_str());
			}
			else
			{
				for(size_t i = 0; i < 3; i++)
				{
					sprintf(name, "%s_%d", info.m_textureName.c_str(), i);
					std::string nameStr(name);
					spriteFrameCache->addSpriteFramesWithFile((nameStr + ".plist").c_str(), (nameStr + ".png").c_str());

					AnimatePacker::getInstance()->loadAnimations((nameStr + ".xml").c_str());
				}
			}
		}
	}
}

const PawnInfo& PawnInfoSet::getPawnInfo(RaceType raceType,PawnType type, int index)const
{
	RacePawnInfoList::const_iterator iterRace = m_racePawnInfos.find(raceType);
	assert(iterRace != m_racePawnInfos.end());
	const PawnInfoList& pawnInfos = iterRace->second;
	PawnInfoList::const_iterator iter = pawnInfos.find(type);
	assert(iter != pawnInfos.end());
	return (iter->second)[index];
}


PawnType PawnInfoSet::getPawnTypeFromStr(const std::string& str)
{
	if(str == "Pawn_Normal")
		return Pawn_Normal;
	else if(str == "Pawn_Wall")
		return Pawn_Wall;
	else if(str == "Pawn_Elite")
		return Pawn_Elite;
	else if(str == "Pawn_Champion")
		return Pawn_Champion;
	else
		return PawnType_Invalid;	
}

RaceType PawnInfoSet::getRaceTypeFromStr(const std::string& str)
{
	if(str == "Human")
		return Race_Human;
	else
		return RaceType_Invalid;	
}